package cn.quevo.cube.api.data;

import cn.quevo.common.annotation.RequiresPermissions;
import cn.quevo.common.entity.ResultEntity;
import cn.quevo.common.enums.ResultStatusEnum;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.core.spring.SpringController;
import cn.quevo.cube.provide.data.entity.CubeModelData;
import cn.quevo.cube.provide.data.entity.query.CubeModelDataQuery;
import cn.quevo.cube.provide.data.service.CubeModelDataService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 数据记录表 RestController
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/12/11 14:57:51
 * @Copyright 湘豫（北京）科技有限公司版权所有.All Rights Reserved.
 */
@RestController("cubeModelDataController")
@RequestMapping("/cube/")
public class CubeModelDataRestController extends SpringController{

    @Resource(name="cubeModelDataService")
    private CubeModelDataService cubeModelDataService;

    /**
     * 分页加载数据记录表
     *
     * @param cubeModelDataQuery
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:57:51
     */
    @RequestMapping(value="cubeModelData",method= RequestMethod.GET)
    @RequiresPermissions("cube:cubeModelData:list")
    public ResultEntity page(CubeModelDataQuery cubeModelDataQuery){
        return cubeModelDataService.getPage(cubeModelDataQuery);
    }

    /**
     * 保存数据记录表
     *
     * @param cubeModelData
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:57:51
     */
    @RequestMapping(value="cubeModelData",method=RequestMethod.POST)
    @RequiresPermissions("cube:cubeModelData:create")
    public ResultEntity create(CubeModelData cubeModelData){
        if(cubeModelDataService.create(cubeModelData) <= 0){
            return new ResultEntity(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }

    /**
     * 修改数据记录表
     *
     * @param cubeModelData
     * @param id
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018/12/11 14:57:51
     */
    @RequestMapping(value="cubeModelData//{id:\\d+}",method=RequestMethod.PUT)
    @RequiresPermissions("cube:cubeModelData:update")
    public ResultEntity update(CubeModelData cubeModelData, @PathVariable("id") Long id){
        if (StringUtils.isBlank(id)) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_UNSELECTED);
        }
        cubeModelData.setId(id);
        if (StringUtils.isBlank(this.cubeModelDataService.update(cubeModelData))) {
            return ResultEntity.fail(ResultStatusEnum.RECORD_REPEAT);
        }
        return ResultEntity.success();
    }
}
